/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.service.core.impl;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.MailTemplate;
import com.blandware.atleap.persistence.core.MailTemplateDAO;
import com.blandware.atleap.service.core.MailTemplateManager;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>Implementation of MailTemplateManager</p>
 * <p><a href="MailTemplateManagerImpl.java.html"><i>View Source</i></a>
 * </p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.5 $ $Date: 2006/08/03 10:07:43 $
 */
public class MailTemplateManagerImpl extends BaseManagerImpl implements MailTemplateManager {

	/**
	 * Mail template DAO
	 */
	protected MailTemplateDAO mailTemplateDAO;

	/**
	 * List of mappings <code>identifier -&gt; list of available variables</code>
	 */
	protected Map mailTemplates;

	/**
	 * Creates new instance of MailTemplateManagerImpl
	 */
	public MailTemplateManagerImpl() {
	}

    /**
     * Sets DAO for operating with mail templates
     *
     * @param mailTemplateDAO the DAO to set
     */
	public void setMailTemplateDAO(MailTemplateDAO mailTemplateDAO) {
		this.mailTemplateDAO = mailTemplateDAO;
	}

    /**
     * Sets list of mappings from template identifiers to lists of available
     * variables
     *
     * @param mailTemplates list to set
     */
	public void setMailTemplates(Map mailTemplates) {
		this.mailTemplates = mailTemplates;
	}

    /**
     * @see com.blandware.atleap.service.core.MailTemplateManager#initialize()
     */
	public void initialize() {
		Set templateIdentifiers = mailTemplates.keySet();

		// delete templates which identifiers are not presented in the list
		List existentTemplates = mailTemplateDAO.listMailTemplates(null).asList();
		for ( Iterator i = existentTemplates.iterator(); i.hasNext(); ) {
			MailTemplate template = (MailTemplate) i.next();
			if ( !templateIdentifiers.contains(template.getIdentifier()) ) {
				mailTemplateDAO.deleteMailTemplate(template);
			}
		}

		// create unexistent templates
		for ( Iterator i = templateIdentifiers.iterator(); i.hasNext(); ) {
			String identifier = (String) i.next();
			MailTemplate template = mailTemplateDAO.findMailTemplateByIdentifier(identifier);
			if ( template == null ) {
				template = new MailTemplate();
				template.setIdentifier(identifier);
				mailTemplateDAO.createMailTemplate(template);
			}
		}

        if (log.isInfoEnabled()) {
            log.info("Mail templates initialized");
        }
	}

	/**
	 * @see com.blandware.atleap.service.core.MailTemplateManager#retrieveMailTemplate(Long)
	 */
	public MailTemplate retrieveMailTemplate(Long templateId) {
		MailTemplate template = mailTemplateDAO.retrieveMailTemplate(templateId);
		if ( template != null ) {
			List availableVariables = (List) mailTemplates.get(template.getIdentifier());
			if ( availableVariables != null ) {
				template.setAvailableVariables(availableVariables);
			}
		}
		return template;
	}

	/**
	 * @see com.blandware.atleap.service.core.MailTemplateManager#updateMailTemplate(com.blandware.atleap.model.core.MailTemplate, java.util.Map)
	 */
	public void updateMailTemplate(MailTemplate mailTemplate, Map linkedObjects) {
        // remove mail template from cache in order to prevent Hibernate from assigning new version number
		mailTemplateDAO.removeFromCache(mailTemplate);

		if ( log.isDebugEnabled() ) {
			log.debug("Updating mail template with ID '" + mailTemplate.getId() + "'...");
		}

		mailTemplateDAO.updateMailTemplate(mailTemplate, linkedObjects);

		if ( log.isDebugEnabled() ) {
			log.debug("Mail template has been updated successfully");
		}
	}

	/**
	 * @see com.blandware.atleap.service.core.MailTemplateManager#listMailTemplates(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listMailTemplates(QueryInfo queryInfo) {
		return mailTemplateDAO.listMailTemplates(queryInfo);
	}

	/**
	 * @see com.blandware.atleap.service.core.MailTemplateManager#findMailTemplateByIdentifier(String)
	 */
	public MailTemplate findMailTemplateByIdentifier(String templateIdentifier) {
		return mailTemplateDAO.findMailTemplateByIdentifier(templateIdentifier);
	}
}
